import 'package:flutter/material.dart';
import '../../models/category_model.dart';
import '../../widgets/tags/cell.dart';

class SimpleDialogPage extends StatefulWidget {
  const SimpleDialogPage({Key? key}) : super(key: key);

  @override
  State<SimpleDialogPage> createState() => _SimpleDialogPageState();
}

class _SimpleDialogPageState extends State<SimpleDialogPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Simple Dialog page'),
      ),
      body: ListView(
        children: <Widget>[
          Center(
            child: ElevatedButton(
              onPressed: () {
                _askedToLead();
              },
              child: const Text('select button'),
            ),
          ),
          Center(
            child: ElevatedButton(
              onPressed: _listDialog,
              child: const Text('diy content'),
            ),
          ),
          Center(
            child: ElevatedButton(
              onPressed: () {
                _neverSatisfied();
              },
              child: const Text('Alert dialog'),
            ),
          ),
          Center(
            child: ElevatedButton(
              onPressed: _widgetDialog,
              child: const Text('diy widget'),
            ),
          ),
        ],
      ),
    );
  }

  Future<void> _askedToLead() async {
    switch (await showDialog<Department>(
        context: context,
        builder: (BuildContext context) {
          return SimpleDialog(
            title: const Text('Select assignment'),
            children: <Widget>[
              SimpleDialogOption(
                onPressed: () {
                  Navigator.pop(context, Department.treasury);
                },
                child: const Text('Treasury department'),
              ),
              SimpleDialogOption(
                onPressed: () {
                  Navigator.pop(context, Department.state);
                },
                child: const Text('State department'),
              ),
            ],
          );
        })) {
      case Department.treasury:
        // Let's go.
        // ...
        break;
    }
  }

  Future<void> _listDialog() async {
    switch (await showDialog<Department>(
        context: context,
        builder: (BuildContext context) {
          var size = MediaQuery.of(context).size;

          return Center(
            child: Container(
                width: size.width * .8,
                height: size.height * .8,
                padding: const EdgeInsets.all(10),
                decoration: const BoxDecoration(
                  color: Colors.blueAccent,
                  shape: BoxShape.rectangle,
                  borderRadius: BorderRadius.all(Radius.circular(5)),
                ),
                //title: const Text('Select assignment'),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: <Widget>[
                    SizedBox(
                      width: size.width * 0.25,
                      child: ListView(
                        primary: false,
                        children: [
                          'a',
                          'b',
                          'c',
                          'd',
                          'b',
                          'c',
                          'd',
                          'b',
                          'c',
                          'd',
                          'b',
                          'c',
                          'd'
                        ].map<TagCell>((item) {
                          return TagCell(
                            icon: const Icon(Icons.add),
                            title: item,
                            desc: item,
                            onTap: () {
                              Navigator.of(context).pop();
                            },
                          );
                        }).toList(),
                      ),
                    ),
                    const SizedBox(
                      width: 1,
                      child: VerticalDivider(
                        thickness: 0.5,
                        color: Colors.blue,
                      ),
                    ),
                    Expanded(
                      child: ListView(
                        primary: false,
                        children: [
                          'a',
                          'b',
                          'c',
                          'd',
                          'b',
                          'c',
                          'd',
                          'b',
                          'c',
                          'd',
                          'b',
                          'c',
                          'd'
                        ].map<TagCell>((item) {
                          return TagCell(
                            icon: const Icon(Icons.add),
                            title: item,
                            desc: item,
                            onTap: () {
                              Navigator.of(context).pop();
                            },
                          );
                        }).toList(),
                      ),
                    ),
                  ],
                )),
          );
        })) {
      case Department.treasury:
        // Let's go.
        // ...
        break;
    }
  }

  Future<void> _widgetDialog() async {
    switch (await showDialog<Department>(
        context: context,
        builder: (BuildContext context) {
          return const PickerWidget();
        })) {
      case Department.treasury:
        // Let's go.
        // ...
        break;
    }
  }

  Future<void> _neverSatisfied() async {
    return showDialog<void>(
      context: context,
      barrierDismissible: false, // user must tap button!
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('Rewind and remember'),
          content: SingleChildScrollView(
            child: ListBody(
              children: const <Widget>[
                Text('You will never be satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
                Text('You’re like me. I’m never satisfied.'),
              ],
            ),
          ),
          actions: <Widget>[
            TextButton(
              child: const Text('Regret'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }
}

class PickerWidget extends StatefulWidget {
  const PickerWidget({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _PickerWidgetState();
}

class _PickerWidgetState extends State<PickerWidget> {
  List<CategoryModel>? cates;

  @override
  void initState() {
    super.initState();

    Future.delayed(const Duration(seconds: 1), () {
      setState(() {
        cates = [
          CategoryModel(),
          CategoryModel(),
          CategoryModel(),
          CategoryModel(),
        ];
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    var size = MediaQuery.of(context).size;
    return cates == null
        ? const Center(child: CircularProgressIndicator())
        : Center(
            child: Container(
              width: size.width * .8,
              height: size.height * .8,
              padding: const EdgeInsets.all(10),
              decoration: const BoxDecoration(
                color: Colors.white,
                shape: BoxShape.rectangle,
                borderRadius: BorderRadius.all(Radius.circular(5)),
              ),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.start,
                children: <Widget>[
                  SizedBox(
                    width: size.width * 0.25,
                    child: ListView(
                      primary: false,
                      children: ['a', 'b', 'c', 'd'].map<TagCell>((item) {
                        return TagCell(
                          icon: const Icon(Icons.add),
                          title: item,
                          desc: item,
                          onTap: () {
                            Navigator.of(context).pop();
                          },
                        );
                      }).toList(),
                    ),
                  ),
                  SizedBox(
                    width: 1,
                    height: size.height,
                    child: const VerticalDivider(
                      color: Colors.blueAccent,
                    ),
                  ),
                  Expanded(
                    child: ListView(
                      primary: false,
                      children: ['a', 'b', 'c', 'd'].map<TagCell>((item) {
                        return TagCell(
                          icon: const Icon(Icons.add),
                          title: item,
                          desc: item,
                          onTap: () {
                            Navigator.of(context).pop();
                          },
                        );
                      }).toList(),
                    ),
                  ),
                ],
              ),
            ),
          );
  }
}

class Department {
  static const Department treasury = Department();

  static const Department state = Department();

  const Department();
}
